package com.foreknow.service.impl;

import com.foreknow.dao.inpl.AdminDaoImpl;
import com.foreknow.dao.inpl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    //1. 链接数据库
    DBManager dbManager= DBManager.getInstance();
    Connection conn =dbManager.getConnection();

    GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();


    //增加
    @Override
    public boolean save(Guestbook guestbook) {
        //调用BaseDao中的Connection(conn)方法
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.add(guestbook);
            //手动提交事务提交事务
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
          //  回滚事务取消之前的操作
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    //删除
    @Override
    public boolean delete(int id) {
        //调用BaseDao中的Connection(conn)方法
        guestbookDao.setConnection(conn);
        boolean delete =false;
        try {
             delete =guestbookDao.delete(id);
            //手动提交事务提交事务
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return delete;
    }

    //修改
    @Override
    public boolean update(Guestbook guestbook) {
        //调用BaseDao中的Connection(conn)方法
        guestbookDao.setConnection(conn);
        Boolean update =null;
        try {
             update= guestbookDao.update(guestbook);
            //手动提交事务提交事务
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return update;
    }

    //根据id查询
    @Override
    public Guestbook Idquery(int id) {
        //调用BaseDao中的Connection(conn)方法
        guestbookDao.setConnection(conn);
        Guestbook Idquery =guestbookDao.Idquery(id);
        return Idquery;
    }

    @Override
    public boolean updates(Guestbook guestbook) {
        //调用BaseDao中的Connection(conn)方法
        guestbookDao.setConnection(conn);
        boolean updates = false;
        try {
            updates= guestbookDao.updates(guestbook);
            //手动提交事务提交事务
             conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return updates;
    }
}
